<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 
 */
class User extends Admin_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin/User_model', 'model');
	}
	public function index()
	{
		$data = [];
		$data['users'] = $this->model->result();
		$this->load->view($this->view, $data);
	}
	public function add()
	{
		$data = [];

		if ('post' == $this->input->method()) {
			$username = $this->input->post('username', TRUE);
			if ($this->model->row(['username' => $username])) {
				$this->jump('用户帐号 '. $username .' 已经存在');
			}
			$insert_id = $this->model->add();
			if (empty($insert_id)) {
				$this->jump();
			}
			$this->jump('操作成功', 2, url('admin/user/add/'. $this->input->post('role_id')));
		}

		$this->load->model('admin/Role_Model');
		$data['role'] = $this->Role_Model->lists();

		$this->load->view($this->view, $data);
	}

	public function edit($id = 0)
	{
		$data = [];

		$id = (int) $id;
		$where = ['id' => $id];

		if ('post' == $this->input->method()) {
			$affected_rows = $this->model->edit($where);
			if (empty($affected_rows)) {
				$this->jump();
			}
			$this->jump('操作成功', 2, url('admin/user/index'));
		}

		$data['row'] = $this->model->row($where);
		if (empty($data['row'])) {
			$this->jump('数据非法');
		}

		$this->load->model('admin/Role_Model');
		$data['role'] = $this->Role_Model->lists();

		$this->load->view($this->view, $data);
	}

	public function del($id = 0)
	{
		$id = (int) $id;

		if (in_array($id, config_item('allow_id'))) {
			redirect('admin/user/index');
		}

		$affected_rows = $this->model->del(['id' => $id]);
		if (empty($affected_rows)) {
			$this->jump();
		}

		redirect('admin/user/index');
	}

	public function password()
	{
		$data = [];

		if ('post' == $this->input->method()) {
			$user = $this->session->userdata('user');
			$post = $this->input->post(NULL, TRUE);

			if (empty($post['old_password'])) {
				$this->jump('请输入原始密码');
			} elseif (user_password($post['old_password']) != $user['password']) {
				$this->jump('原始密码错误');
			}

			if (empty($post['new_password'])) {
				$this->jump('请输入新的密码');
			}

			if ($post['new_password'] != $post['confirm_password']) {
				$this->jump('确认密码必须与新的密码一致');
			}

			$where = ['id' => $user->id];
			$data = ['password' => user_password($post['new_password'])];
			$affected_rows = $this->model->edit($where, $data);
			if (empty($affected_rows)) {
				$this->jump();
			}
			$user->password = $data['password'];
			$this->session->set_userdata('user', $user);
			$this->jump('操作成功', 2, site_url('admin/user/index'));
		}

		$this->load->view($this->view, $data);
	}
	public function login()
	{
		$data = [];
		if ($this->input->is_ajax_request()) {
			$post = $this->input->post(NULL, TRUE);

			if (empty($post['user'])) {
				exit(json_encode([
					'status' => 0,
					'message' => '请输入帐号',
				]));
			}

			if (empty($post['pass'])) {
				exit(json_encode([
					'status' => 0,
					'message' => '请输入密码',
				]));
			}

			if (empty($post['captcha'])) {
				exit(json_encode([
					'status' => 0,
					'message' => '请输入验证码',
				]));
			}

			if (strtoupper($post['captcha']) != strtoupper($this->session->userdata('captcha'))) {
				exit(json_encode([
					'status' => 0,
					'message' => '验证码错误',
				]));
			}

			$data = [
				'username' => $post['user'],
				'password' => user_password($post['pass']),
			];
			$user = $this->model->row($data, [], true);
			if (empty($user)) {
				exit(json_encode([
					'status' => 0,
					'message' => '帐号或密码错误 ',
				]));
			}

			$where = ['id' => $user->id];
			$data = [
				'login_time' => time(),
				'login_num' => $user->login_num + 1,
				'login_ip' => $this->input->ip_address(),
			];
			$this->model->update($where, $data);

			$this->session->set_userdata('User', $user);
			$this->session->unset_userdata('captcha');

			exit(json_encode([
				'status' => 1,
				'message' => '登录成功',
				'href' => site_url('admin/main/index'),
			]));
		}
		$data['captcha'] = $this->captcha();
		$this->load->view('admin/user/login', $data);
	}
	public function logout() {
		$this->session->unset_userdata('User');
		redirect('admin');
	}
}